package com.wind.task;

import java.util.HashMap;
import java.util.Map;

import com.wind.config.TelAlarmConfig;
import com.wind.util.StringUtil;
import com.wind.util.sms.BaseTest;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;


import com.wind.util.Tools;

import javax.annotation.Resource;
//jiaquanzhen
//https://blog.csdn.net/u011179993/article/details/74791304
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class DbTask {
    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
//    private String key_words="您好欢迎致电商家";//判断的关键字
    private String error_words="您好，您的4008电话出现异常";

    private String[] key_words= {
            "商家号码","自助认证","来电名片","绑定"
    };//判断的关键字


    private String tel1="18930091151";  //短信电话1
    private String tel2="18049880825"; //短信电话2
    private String tel3="17717089983"; //短信电话2

   // private String tel[]= TelAlarmConfig.tel_alarm_num;
           // {"18930091151","18049880825","17717089983"};  //短信电话1
    @Mapper
    public void select() {
    	//state 0 未拨打，1正在拨打，2拨打结束
    	String sql ="SELECT * FROM zjz_message WHERE state=2 and yw_id='01'ORDER BY id  limit 1 ";
//    	 List<Map<String, Object>> reuslt=jdbcTemplate.queryForList(sql, new HashMap());
    	Map< String, Object> reuslt =null;
    	try {
    		reuslt= jdbcTemplate.queryForMap(sql, new HashMap<>());
    	}catch (Exception e) {
		}
    	if(reuslt!=null&&reuslt.get("id")!=null) {
    		Integer message_id=Integer.valueOf(reuslt.get("id").toString());
    		String detail_sql ="SELECT * FROM zjz_bd_log WHERE message_id="+message_id+"  ORDER BY id DESC  LIMIT 1 ";
    		Map< String, Object> detail_reuslt = jdbcTemplate.queryForMap(detail_sql, new HashMap<>());
            String rec_url;
            String file_name;
            String wz_json;
            try {
                rec_url=detail_reuslt.get("rec_url").toString();
                file_name=rec_url.replace("D:/wamp64/www/donjin/wav/", "");
                System.out.println(rec_url);
                System.out.println(file_name);
                wz_json = Tools.getVoiceContent(rec_url,file_name);
            }
            catch (Exception ex )
            {
                wz_json=ex.toString();
                rec_url=ex.toString();
            }


                String reg = "[^\u4e00-\u9fa5]";
                String wz = StringUtil.getStr(wz_json).replaceAll(reg, "");
                int add_result=add(message_id,rec_url,wz,wz_json);
                System.out.println("---------add_result--------"+add_result);

                if(add_result>0) {
                    update(message_id);
                }

                int keywordfind= 0;
                for (String tempkey_words:key_words
                        ) {
                    if( wz.indexOf(tempkey_words)==-1)
                    {
                        keywordfind++;
                    }
                }





    		if(keywordfind>2) {
                String teltempstr ="";
                String resulttemp="";
                for (String teltemp:TelAlarmConfig.tel_alarm_num
                     ) {
                    BaseTest testtemp=new BaseTest(teltemp,"118114",error_words);
                    String resultt=testtemp.doSend();
                    teltempstr =teltempstr +"|"+teltemp;
                    resulttemp=resulttemp+"|"+resultt;
                }
                update_wz(message_id,teltempstr.toString(),resulttemp.toString());
    		}
    	}
    }
    
    public int add(Integer message_id,String rec_url,String wz,String wz_json) {
        String sql = "insert into zjz_message_wz(state,message_id,rec_url,wz,wz_json) " +
                "values(2,:message_id,:rec_url,:wz,:wz_json)";
        Map<String, Object> param = new HashMap<>();
        param.put("message_id",message_id);
        param.put("rec_url", rec_url);
        param.put("wz", wz);
        param.put("wz_json", wz_json);
        return (int) jdbcTemplate.update(sql, param);
    }
    
    public int update(Integer message_id) {
        String sql = "UPDATE zjz_message SET state=3 WHERE id=:id" ;
        Map<String, Object> param = new HashMap<>();
        param.put("id", message_id);
        return (int) jdbcTemplate.update(sql, param);
    }
    
    public int update_wz(Integer message_id ,String sms_tel,String  sms_str) {
        String sql = "UPDATE zjz_message_wz SET sms_tel=:sms_tel, sms_str=:sms_str WHERE message_id=:message_id" ;
        Map<String, Object> param = new HashMap<>();
        param.put("message_id", message_id);
        param.put("sms_tel", sms_tel);
        param.put("sms_str", sms_str);
        return (int) jdbcTemplate.update(sql, param);
    }


    //3.添加定时任务
    @Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {
    	select();
    }
/**
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource()
        .addValue("id", UUID.randomUUID().toString())
        .addValue("name", "小王")
        .addValue("homeAddress", "美国");
template.update("insert into student(id,name,home_address) values    
                   (:id,:name,:homeAddress)",mapSqlParameterSource);

 */
public static void main(String[] args) {
    int keywordfind= 0;
    String wz="大地方都是";
    String[] key_words=new String[] {"大","生","你","烧"};
    for (String tempkey_words:key_words
            ) {
        if( wz.indexOf(tempkey_words)==-1)
        {
            keywordfind++;
        }
    }

    if(keywordfind>2) {
        System.out.println("发短信");

    }
}

}